login.js ➔ login   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 67
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 67
rs 5.9999
c 0
b 0
f 0
cc 10

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like login.js ➔ login often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
var express = require('express');
2
var router = express.Router();
3
// const config = require('../config');
4
const sqlite3 = require('sqlite3').verbose();
0 ignored issues
show
Unused Code introduced by
The constant sqlite3 seems to be never used. Consider removing it.
Loading history...
5
// const db = new sqlite3.Database('./db/texts.sqlite');
6
const db = require("../db/database.js");
7
const bcrypt = require('bcryptjs');
8
const jwt = require('jsonwebtoken');
9
10
let config;
11
12
try {
13
    config = require('../config.js');
14
} catch (error) {
15
    console.error(error);
16
}
17
18
const jwtSecret = process.env.JWT_SECRET || config.secret;
19
20
router.get('/', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
21
    const data = {
22
        data: {
23
            msg:  "Login a user"
24
        }
25
    };
26
27
    res.json(data);
28
});
29
30
router.post("/", (req, res) => {
31
    login(res, req.body);
32
33
});
34
35
function login(res, body) {
36
    const email = body.email;
37
    const password = body.password;
38
39
    if (!email || !password) {
40
        return res.status(401).json({
41
            errors: {
42
                status: 401,
43
                source: "/login",
44
                title: "Email or password missing",
45
                detail: "Email or password missing in request"
46
            }
47
        });
48
    }
49
50
    db.get("SELECT * FROM users WHERE email = ?",
51
        email,
52
        (err, rows) => {
53
        if (rows === undefined) {
54
            return res.status(401).json({
55
                errors: {
56
                    status: 401,
57
                    source: "/login",
58
                    title: "User not found",
59
                    detail: "User with provided email not found."
60
                }
61
            });
62
        }
63
        const user = rows;
64
65
        bcrypt.compare(password, user.password, (err, result) => {
66
            if (err) {
67
                return res.status(500).json({
68
                    errors: {
69
                        status: 500,
70
                        source: "/login",
71
                        title: "bcrypt error",
72
                        detail: "bcrypt error"
73
                    }
74
                });
75
            }
76
77
            if (result) {
78
                let payload = { email: user.email };
79
                let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' });
80
81
                return res.json({
82
                    data: {
83
                        type: "success",
84
                        message: "User logged in",
85
                        user: payload,
86
                        token: jwtToken
87
                    }
88
                });
89
            }
90
91
            return res.status(401).json({
92
                errors: {
93
                    status: 401,
94
                    source: "/login",
95
                    title: "Wrong password",
96
                    detail: "Password is incorrect."
97
                }
98
            });
99
        });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
100
    });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
101
}
102
103
module.exports = router;
104